home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / d2_dicti.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.4 KB  |  133 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  d2_dictionary.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17. #ifndef D2_DICTIONARY_H
  18. #define D2_DICTIONARY_H
  19.  
  20. #include <LEDA/range_tree.h>
  21.  
  22.  
  23. typedef range_tree_item dic2_item;
  24.  
  25. typedef list(range_tree_item) list(dic2_item);
  26.  
  27.  
  28. // -------------------------------------------------------------------
  29. // 2-dim dictionary
  30. // -------------------------------------------------------------------
  31.  
  32. #define d2_dictionary(type1,type2,itype) name4(type1,type2,itype,d2_dictionary)
  33. #define d2_dict(type1,type2,itype) name4(type1,type2,itype,d2_dict)
  34.  
  35.  
  36. #define d2_dictionarydeclare3(type1,type2,itype)\
  37. \
  38. struct d2_dict(type1,type2,itype) : public range_tree {\
  39. \
  40. void clear_key(ent& x) const  { Clear(*(type1*)&(*(tuplep&)x)[0]);\
  41.                                 Clear(*(type2*)&(*(tuplep&)x)[1]); }\
  42. \
  43. void clear_inf(ent& x) const  { Clear(*(itype*)&(*(tuplep&)x).info());}\
  44. \
  45. void copy_key(ent& x)  const  { Copy(*(type1*)&(*(tuplep)x)[0]);\
  46.                                 Copy(*(type2*)&(*(tuplep)x)[1]); }\
  47. \
  48. void copy_inf(ent& x)  const  { Copy(*(itype*)&(*(tuplep)x).info());}\
  49. \
  50. int cmp_tuple_entry(int i,tuplep p, tuplep q)\
  51. { if (i==1) return compare(*(type1*)&(*p)[0],*(type1*)&(*q)[0]);\
  52.   else      return compare(*(type2*)&(*p)[1],*(type2*)&(*q)[1]);\
  53.  }\
  54. \
  55. range_tree* new_range_tree(int dim)\
  56. { return new d2_dict(type1,type2,itype)(dim); }\
  57. \
  58. type1 key1(dic2_item x)   { return type1((*x)[0]);  }\
  59. type2 key2(dic2_item x)   { return type2((*x)[1]);  }\
  60. itype inf(dic2_item x)    { return itype(x->info());}\
  61. \
  62. void  change_inf(dic2_item x, itype i) { *(itype*)&(x->info()) = i; }\
  63. \
  64. dic2_item min_key1()       { return range_tree::min(1); }\
  65. dic2_item max_key1()       { return range_tree::max(1); }\
  66. dic2_item min_key2()       { return range_tree::min(2); }\
  67. dic2_item max_key2()       { return range_tree::max(2); }\
  68. \
  69. \
  70. dic2_item insert(type1 x,type2 y,itype i)\
  71. { tuplep p = new tuple(Copy(x),Copy(y));\
  72.   p->info() = Copy(i);\
  73.   return range_tree::insert_tuple(p);\
  74.  }\
  75. \
  76. void del(type1 x,type2 y)\
  77. { tuple p(Ent(x),Ent(y));\
  78.   range_tree::del(&p);\
  79.  }\
  80. \
  81. void del_item(dic2_item it)\
  82. { range_tree::del(it);\
  83.  }\
  84. \
  85. list(dic2_item) all_items()\
  86. { return all_tuples();\
  87.  }\
  88. \
  89. list(dic2_item) range_search(type1 x0,type1 x1,type2 y0,type2 y1)\
  90. { list(dic2_item) L;\
  91.   tuple p(Ent(x0),Ent(y0));\
  92.   tuple q(Ent(x1),Ent(y1));\
  93.   range_tree::query_tuples(&p,&q,L);\
  94.   return L;\
  95.  }\
  96. \
  97. dic2_item lookup(type1 x,type2 y)\
  98. { tuple q(Ent(x),Ent(y));\
  99.   q.set_cmp(1);\
  100.   bb_tree_item p=rm_tree::lookup(&q);\
  101.   dic2_item r = ( p ? key(p) : 0);\
  102.   return r;\
  103.  }\
  104. \
  105. d2_dict(type1,type2,itype)(int dim = 2) : range_tree(dim) { }\
  106. \
  107. ~d2_dict(type1,type2,itype)() { clear(); }\
  108. \
  109. } ;\
  110. \
  111. struct d2_dictionary(type1,type2,itype) : public d2_dict(type1,type2,itype) {\
  112. \
  113. list(dic2_item) L;\
  114. \
  115. void init_iteration()\
  116. { L = all_tuples();\
  117.  }\
  118. \
  119. d2_dictionary(type1,type2,itype)()  {}\
  120. ~d2_dictionary(type1,type2,itype)() {}\
  121. \
  122. }; 
  123.  
  124. // --------------------------------------------------------------------
  125. // iteration
  126. // --------------------------------------------------------------------
  127.  
  128. #define forall_dic2_items(x,T)  (T).init_iteration(); forall(x,(T).L )
  129.  
  130.  
  131. #endif
  132.